package top.yokey.android.base;

import android.app.Application;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.util.TypedValue;

import androidx.core.content.ContextCompat;

import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.tools.PictureFileUtils;

import java.util.HashMap;

import top.yokey.android.R;
import top.yokey.android.http.HttpHelp;
import top.yokey.android.http.MemberBean;
import top.yokey.android.view.PullRefreshViewHelper;

public class App extends Application {

    private static App app;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private MemberBean memberBean;//这是用户信息数据

    public static App get() {
        return app;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        //初始化Android系统自身的一些框架
        sharedPreferences = getSharedPreferences("wechat", MODE_PRIVATE);
        editor = sharedPreferences.edit();
        //第三方框架，应用自定义框架，系统通用的类，放在此处进行初始化
        //初始化httpHelp
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("model", Build.MODEL);
        hashMap.put("token", getSharedPreferences().getString("token", ""));
        HttpHelp.get().init(this, "http://192.168.8.61/api/", hashMap);
        PictureFileUtils.deleteCacheDirFile(get(), PictureMimeType.ofAll());
        //下拉刷新
        PullRefreshViewHelper.get().init(14);
        PullRefreshViewHelper.get().setColors(ContextCompat.getColor(this,R.color.primary));
        PullRefreshViewHelper.get().setLoadStyle(PullRefreshViewHelper.LOAD_STYLE_DOUBLE);
        PullRefreshViewHelper.get().setLoadColor(ContextCompat.getColor(this,R.color.primary));
        PullRefreshViewHelper.get().setLoadText("加载中~");
        PullRefreshViewHelper.get().setEmptyImage(R.mipmap.ic_pull_refresh_empty);
        PullRefreshViewHelper.get().setEmptyColor(Color.GRAY);
        PullRefreshViewHelper.get().setEmptyText("当前暂无动态，请稍后在刷新试试~");
        PullRefreshViewHelper.get().setErrorImage(R.mipmap.ic_pull_refresh_error);
        PullRefreshViewHelper.get().setErrorColor(Color.GRAY);
        PullRefreshViewHelper.get().setErrorText("加载失败，请稍后再试~");
    }

    public SharedPreferences getSharedPreferences() {
        return sharedPreferences;
    }

    public SharedPreferences.Editor getEditor() {
        return editor;
    }

    public MemberBean getMemberBean() {
        return memberBean;
    }

    public void setMemberBean(MemberBean memberBean) {
        this.memberBean = memberBean;
    }

    //获取屏幕宽度
    public int getWidth() {

        return getResources().getDisplayMetrics().widthPixels;

    }

    //DP转换为像素
    public int dp2Px(int dp) {

        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());

    }

}
